$(document).ready(function(){	
    $('#heading').after($('.info'));
    $('.wishlistpub-pane').append('<ul id="wishitems"></ul>');
    
    $('#wishlistpub div:not(".info,.buy-button-normal") li:not(".gifted,.name,.EventDate,.Code")').appendTo( $('#wishitems') );
    $('#wishitems li:last').css({ 'margin-right' : 0 });
    $('.wishlistpub-pane div ul:empty').remove();
    $('.wishlistpub-pane div:empty').remove();
    
    Cufon.replace('#wishlistpub div:not(".info,.buy-button-normal") li:not(".gifted,.name,.EventDate,.Code")', {
        fontFamily: 'Proxima Nova Rg'
    });
    
	//SCROLLPANE
	$('#wishlistpub ul li a img').attr({"width":"229","height":"229"});
	
	/*Cufon.replace('#wishlistpub ul li a .produto-nome, #wishlistpub .n2colunas ul li a .produto-preco', {
		fontFamily: 'Proxima Nova Rg'
	});	*/
	
	$('#wishlistpub ul').find("li").each(function(){
	
		var limited_edition = false;
		var lancamento = false;
	
		if( $(this).find(".produto-lancamento").length == 1 ){
			limited_edition = true;
		}
		if( $(this).find(".produto-limited").length == 1 ){
			lancamento = true;
		}
		
		if(lancamento == true && limited_edition == true){
			$(this).find("a").append("<img class='seal-img' src='/Control/ArquivoExibir.aspx?NomeArquivo=img_lancamento_limitedEdition.png' />");
		}else{
			if(lancamento == true){
				$(this).find("a").append("<img class='seal-img' src='/Control/ArquivoExibir.aspx?NomeArquivo=img_lancamento.png' />");
			}else{
				if(limited_edition == true){
					$("<img class='seal-img' src='/Control/ArquivoExibir.aspx?NomeArquivo=img_limitedEdition.png' />").appendTo($(this).find("a"));
				}
			}
		}
	});
	
	$('#wishlistpub ul li a').hover(function(){
		if($(this).find(".overlay").length == 0 ){
			$("<div class='overlay'></div>").css("opacity", "0.75").appendTo(this);
			$(this).find(".overlay, .produto-nome, .produto-preco, .seal-img").css("display", "block");
		}
	}, function(){
		$(this).find(".overlay, .produto-nome, .produto-preco, .seal-img").css("display", "none");
		$(this).find(".overlay").remove();
	});
	
	if( $("#wishlistpub ul li").size()<=4 ){
		$("#wishlistpub .scroll-bar").hide();
		$("#selectui").hide();
	}
	    
	//scrollpane parts
	var scrollPane = $( "#wishlistpub" );
    
	var scrollContent = $('#wishlistpub .wishlistpub-pane');
    var innerwidth = ( $('#wishlistpub ul').find("li").size() * 239 ) - 4 ;
    $(scrollContent).width(innerwidth);
	
	//build slider
	var scrollbar = $( ".scroll-bar" ).slider({
		slide: function( event, ui ) {
			if ( scrollContent.width() > scrollPane.width() ) {
				scrollContent.css( "margin-left", Math.round(
					ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
				) + "px" );
			}else {
				scrollContent.css( "margin-left", 0 );
			}
		}
	});
	
	//append icon to handle
	var handleHelper = scrollbar.find( ".ui-slider-handle" )
	.mousedown(function() {
		scrollbar.width( handleHelper.width() );
	})
	.mouseup(function() {
		scrollbar.width( "100%" );
	})
	.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
	.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
	
	//change overflow to hidden now that slider handles the scrolling
	scrollPane.css( "overflow", "hidden" );
	
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
		
		scrollbar.find( ".ui-slider-handle" ).css({
			width: handleSize
		});
		handleHelper.width( "" ).width( scrollbar.width() - handleSize );
	}

	//reset slider value based on scroll content position
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
			parseInt( scrollContent.css( "margin-left" ) );
		var percentage = Math.round( leftVal / remainder * 100 );
		scrollbar.slider( "value", percentage );
	}

	//if the slider is 100% and window gets larger, reveal content
	function reflowContent() {
			var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
			var gap = scrollPane.width() - showing;
			if ( gap > 0 ) {
				scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
			}
	}
	
	//change handle position on window resize
	$( window ).resize(function() {
		resetValue();
		sizeScrollbar();
		reflowContent();
	});
	//init scrollbar size
	setTimeout( sizeScrollbar, 10 );//safari wants a timeout
    
    $('#wishlistpub').fadeIn();
    
});